package com.maiji.cloud.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.maiji.cloud.entities.login.UploadRecord;
import com.maiji.cloud.microservice.GoodsService;
import com.maiji.cloud.request.BaseDataReqDto;
import com.maiji.cloud.request.ReqMetaData;
import com.maiji.cloud.response.BaseResDto;
import com.maiji.cloud.response.shopingmall.GoodsResData;
import com.maiji.cloud.service.UploadRecordService;
import com.maiji.cloud.utils.BaseService;
import com.maiji.cloud.utils.UUID_MD5;
import org.eclipse.jetty.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.maiji.cloud.entities.login.FunctionMemu;
import com.maiji.cloud.mapper.FunctionMemuMapper;
import com.maiji.cloud.response.login.FunctionMenuResDto;
import com.maiji.cloud.response.login.HomeClassify;
import com.maiji.cloud.response.login.MaijiVideo;
import com.maiji.cloud.response.shopingmall.BannerImgsResData;
import com.maiji.cloud.response.shopingmall.GetShoppingBannerImgsResData;
import com.maiji.cloud.response.shopingmall.GetShoppingBannerImgsResDto;
import com.maiji.cloud.response.BaseDataResDto;
import com.maiji.cloud.response.Status;
import com.maiji.cloud.response.login.FunctionMenuResDto.FunctionMenuResData;
import com.maiji.cloud.service.MenuService;
import com.maiji.cloud.utils.CommonStringValueEnum;

@Service
@Transactional
public class MenuServiceImpl extends ServiceImpl<FunctionMemuMapper, FunctionMemu> implements MenuService{

	private static final Logger logger = LoggerFactory.getLogger(MenuServiceImpl.class);
	@Autowired
	private FunctionMemuMapper functionMemuMapper;
	@Autowired
	private UploadRecordService uploadRecordService;
	@Autowired
	private GoodsService goodsService;
	
	@Override
	public FunctionMenuResDto getFunctionMenus() {
		
		List<String> paramList = new ArrayList<String>();
		paramList.add(CommonStringValueEnum.FUNCYION_MENUE_TYPE_INTRODUCE.getValue());
		paramList.add(CommonStringValueEnum.FUNCYION_MENUE_TYPE_RECORD.getValue());
		paramList.add(CommonStringValueEnum.FUNCYION_MENUE_TYPE_FOODSEARCH.getValue());
		paramList.add(CommonStringValueEnum.FUNCYION_MENUE_TYPE_RECIPEREC.getValue());
		
		List<FunctionMemu> list = functionMemuMapper.selectAllHom(paramList);
		FunctionMenuResDto functionMenuResDto = new FunctionMenuResDto(Status.SUCCESS);
		
		FunctionMenuResData data = functionMenuResDto.dataInstance();
		
		List<HomeClassify> homeClassifyList  = new ArrayList<HomeClassify>();
		
		for(FunctionMemu functionMemu : list) {
			HomeClassify homeClassify = new HomeClassify();
			BeanUtils.copyProperties(functionMemu, homeClassify);
			homeClassifyList.add(homeClassify);
		}
		
		data.setHomeClassify(homeClassifyList);
		
		FunctionMemu functionMemuOne = functionMemuMapper.selectOneView(CommonStringValueEnum.FUNCYION_MENUE_TYPE_VIEW.getValue());
		MaijiVideo maijiVideo  = new MaijiVideo();
		if(functionMemuOne != null) {
			BeanUtils.copyProperties(functionMemuOne, maijiVideo);
		}
		
		
		data.setMaijiVideo(maijiVideo);
		
		return functionMenuResDto.setData(data);
	}

	@Override
	public GetShoppingBannerImgsResDto getShoppingBannerImgs() {
		List<FunctionMemu> list = functionMemuMapper.selectListBannerImgs(CommonStringValueEnum.FUNCYION_MENUE_TYPE_SHOPPING_BANNNER.getValue());
		GetShoppingBannerImgsResDto getShoppingBannerImgsResDto = new GetShoppingBannerImgsResDto(Status.SUCCESS);
		List<GetShoppingBannerImgsResData> listGetShoppingBannerImgsResData = new ArrayList<GetShoppingBannerImgsResData>();
		for(FunctionMemu functionMemu : list) {
			GetShoppingBannerImgsResData GetShoppingBannerImgsResData = new GetShoppingBannerImgsResData();
			BeanUtils.copyProperties(functionMemu, GetShoppingBannerImgsResData);
			listGetShoppingBannerImgsResData.add(GetShoppingBannerImgsResData);
		}
		getShoppingBannerImgsResDto.setData(listGetShoppingBannerImgsResData);
		return getShoppingBannerImgsResDto;
	}

	@Override
	public FunctionMenuResDto getAppletFunctionMenus() {
		List<String> paramList = new ArrayList<String>();
		paramList.add(CommonStringValueEnum.FUNCYION_MENUE_TYPE_APPLET_INTRODUCE.getValue());
		paramList.add(CommonStringValueEnum.FUNCYION_MENUE_TYPE_APPLET_FOODSEARCH.getValue());
		paramList.add(CommonStringValueEnum.FUNCYION_MENUE_TYPE_APPLET_RECIPEREC.getValue());
		
		List<FunctionMemu> list = functionMemuMapper.selectAllHom(paramList);
		FunctionMenuResDto functionMenuResDto = new FunctionMenuResDto(Status.SUCCESS);
		
		FunctionMenuResData data = functionMenuResDto.dataInstance();
		
		List<HomeClassify> homeClassifyList  = new ArrayList<HomeClassify>();
		
		for(FunctionMemu functionMemu : list) {
			HomeClassify homeClassify = new HomeClassify();
			BeanUtils.copyProperties(functionMemu, homeClassify);
			homeClassifyList.add(homeClassify);
		}
		
		data.setHomeClassify(homeClassifyList);
		
		FunctionMemu functionMemuOne = functionMemuMapper.selectOneView(CommonStringValueEnum.FUNCYION_MENUE_TYPE_VIEW.getValue());
		MaijiVideo maijiVideo  = new MaijiVideo();
		if(functionMemuOne != null) {
			BeanUtils.copyProperties(functionMemuOne, maijiVideo);
		}
		
		
		data.setMaijiVideo(maijiVideo);
		
		return functionMenuResDto.setData(data);
	}

	@Override
	public BaseDataResDto<List<BannerImgsResData>> getBannerImgsByType(String type) {
		List<FunctionMemu> list = functionMemuMapper.selectListBannerImgs(type);
		BaseDataResDto<List<BannerImgsResData>> resposne = new BaseDataResDto<List<BannerImgsResData>>(Status.SUCCESS);
		List<BannerImgsResData> listBannerImgsResData = list.stream().map(functionMemu ->{
			BannerImgsResData bannerImgsResData = new BannerImgsResData();
			BeanUtils.copyProperties(functionMemu, bannerImgsResData);
			return bannerImgsResData;
		}).collect(Collectors.toList());
		resposne.setData(listBannerImgsResData);
		return resposne;
	}

	@Override
	public BaseDataResDto<List<FunctionMemu>> findAllFunctionMemus(List<String> types) {
        List<FunctionMemu> functionMemus = selectList(new EntityWrapper<FunctionMemu>()
                .in("type", types).eq("status", 0));
        List<String> goodsIds = functionMemus.parallelStream().filter(fm -> StringUtil.isNotBlank(fm.getGoodsId()))
                .map(FunctionMemu::getGoodsId).collect(Collectors.toList());
        if (goodsIds.size() > 0) {
            List<GoodsResData> goodsList = goodsService.findAllGoodsByIds(new BaseDataReqDto<List<String>>()
                    .setData(goodsIds).setMetaData(new ReqMetaData(1, goodsIds.size()))).getData();
            functionMemus = BaseService.dealWithOneToOne(functionMemus, "getGoodsId", FunctionMemu.class,
                    "setGoods", goodsList, "getUuId", GoodsResData.class);
        }
        return new BaseDataResDto<List<FunctionMemu>>(Status.SUCCESS).setData(functionMemus);
    }

    @Override
    public BaseResDto editFunctionMemu(FunctionMemu functionMemu) {
	    if (StringUtil.isBlank(functionMemu.getUuId())) {
	        functionMemu.setUuId(UUID_MD5.getUUID()).setStatus("0");
	        if (! insert(functionMemu)) return new BaseResDto(Status.ERROR);
            UploadRecord uploadRecord = new UploadRecord().setUuId(functionMemu.getImgId()).setBelongId(functionMemu.getUuId());
            if (! uploadRecordService.updateById(uploadRecord)) return new BaseResDto(Status.ERROR);
         } else if (! updateById(functionMemu)) return new BaseResDto(Status.ERROR);
	    return new BaseResDto(Status.SUCCESS);
    }

}
